package com.tang.commodity.controller;

/*
 *文件名: ClientController
 *创建者: 醉意丶千层梦
 *创建时间:2021/12/13 13:16
 *描述: 这是一个示例
 */

import com.tang.commodity.entity.Client;
import com.tang.commodity.service.ClientService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ClientController {
    @Autowired
    private ClientService clientService;
    private static Logger logger = LogManager.getLogger(ClientController.class);


    @ResponseBody
    @RequestMapping(value = "/client/insert",method = RequestMethod.POST)
    public int insertClient(@RequestBody Client client){
        if (client==null || client.getNo()==null || client.getNo().isEmpty()){
            logger.info("insert client error");
            return -1;
        }

        logger.info("insert client :" +client);
        return clientService.insertClient(client);
    }

    @ResponseBody
    @RequestMapping(value = "/client/delete",method = RequestMethod.POST)
    public int deleteClient(String no){
        if (no==null || no.isEmpty()){
            logger.info("delete client error");
            return -1;
        }
        logger.info("delete client with no : "+no);
        clientService.deleteClient(no);
        return 1;
    }

    @ResponseBody
    @RequestMapping(value = "/client/update",method = RequestMethod.POST)
    public int updateClient(@RequestBody Client client){
        if (client==null ||client.getNo()==null ){
            logger.info("update client error ");
            return 0;
        }
        logger.info("update client with client : " +client);

        return clientService.updateClient(client);
    }

    @ResponseBody
    @RequestMapping(value = "/client/search",method = RequestMethod.POST)
    public List<Client> searchClient(String  key){
        if (key==null)
            key="";
        logger.info("search client for key : " +key);
        return clientService.searchClients(key);
    }

    @ResponseBody
    @RequestMapping(value = "/client/find",method = RequestMethod.POST)
    public Client searchClient(Client  client){
        if (client==null || client.getNo()==null){
            logger.info("find client error ");
            return null;
        }

        logger.info("find the client :"+client);
        return clientService.findClient(client);
    }

    @ResponseBody
    @RequestMapping(value = "/client/get",method = RequestMethod.POST)
    public Client getClient(String  no){
        if (no==null ){
            logger.info("get client error ");
            return null;
        }

        logger.info("get the client :"+no);
        return clientService.getClient(no);
    }

}
